"""
Descripttion:
Version: 1.0
Author: ZhangHongYu
Date: 2021-03-08 14:48:29
LastEditors: ZhangHongYu
LastEditTime: 2021-03-08 15:01:59
"""
import numpy as np
import math


def bi_sect(f, /, start, end, tol):
    m_tol: float = 1e-15
    if f(start) * f(end) >= 0:
        raise ValueError("fun(a_)*fun(b)<0 not satisfied!")
    while (end - start) / 2.0 > tol:
        c = float(start + end) / 2.0  # 即使a和b是int,此处c自动转float了
        if math.fabs(f(c) - 0) <= m_tol:  # c是一个解，完成
            break
        if f(start) * f(c) < 0:
            end = c
        else:
            start = c
    return (start + end) / 2


if __name__ == '__main__':
    res = bi_sect(lambda x: x ** 3 + x - 1, start=0, end=1, tol=5 * pow(10, -5))
    print(res)
